﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;

namespace TurboTemplate
{
    [DataContract()]
    public class GeneratorConfiguration
    {

        private List<string> m_ConnectionStrings;

        [DataMember(), Browsable(false)]
        public List<string> ConnectionStrings
        {
            get
            {
                if (m_ConnectionStrings == null)
                    m_ConnectionStrings = new List<string>();
                return m_ConnectionStrings; 
            }
            set { m_ConnectionStrings = value; }
        }

        [DataMember()]
        public bool UseTransaction { get; set; }

        [DataMember()]
        public string Syntax { get; set; }


        [DataMember()]
        public System.Drawing.Size WindowSize { get; set; }
        [DataMember()]
        public System.Drawing.Point WindowLocation { get; set; }
        [DataMember()]
        public System.Windows.Forms.FormWindowState WindowState { get; set; }
        [DataMember()]
        public int MainLRSplitDistance { get; set; }
        [DataMember()]
        public int SourcesSplitDistance { get; set; }
        [DataMember()]
        public Orientation TemplateSplitOrientation { get; set; }
        [DataMember()]
        public int TemplateSplitDistance { get; set; }
        [DataMember()]
        public string LastLoadedProfile { get; set; }
        [DataMember()]
        public int SourceTabIndex { get; set; }

        #region Loading/Saving
        private static GeneratorConfiguration m_Instance;

        public static GeneratorConfiguration Instance
        {
            get { return m_Instance; }
        }

        public static string SettingsFileName
        {
            get
            {
                string path = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                path = System.IO.Path.Combine(path, "SprocToSqlCommand");
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                return System.IO.Path.Combine(path, "config.xml");
            }
        }

        public static void LoadConfiguration()
        {
            try
            {
                m_Instance = Load<GeneratorConfiguration>();
            }
            catch
            {
                m_Instance = new GeneratorConfiguration();
            }
        }

        public static void SaveConfiguration()
        {
            Save<GeneratorConfiguration>();
        }

        public static T Load<T>() where T : class, new()
        {
            string file = SettingsFileName;
            DataContractSerializer dc = new DataContractSerializer(typeof(T));

            using (var f = System.IO.File.Open(file, System.IO.FileMode.Open))
            {
                return dc.ReadObject(f) as T;
            }
        }

        public static void Save<T>()
        {
            string file = SettingsFileName;
            DataContractSerializer dc = new DataContractSerializer(typeof(T));

            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(file, false))
            {
                using (System.Xml.XmlTextWriter tw = new System.Xml.XmlTextWriter(sw))
                {
                    tw.Formatting = System.Xml.Formatting.Indented;
                    dc.WriteObject(tw, m_Instance);
                }
            }

        }
        #endregion
    }
}
